package com.atguigu.lease.web.admin.task;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class CheckAndUpdateAgreementStatusTask {

    @Autowired
    private LeaseAgreementService leaseAgreementService;

//    @Scheduled(cron = "0 0 0 * * ?")
@Scheduled(cron = "0 05 15 * * ?")
    public void checkAndUpdate(){
        //1.创建LambdaUpdateWrapper对象
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //2.封装查询条件
        //2.1 租约结束日期，小于等于当前日期
        lambdaUpdateWrapper.le(LeaseAgreement::getLeaseEndDate,new Date());
        //2.2 租约状态是2,5,7
        lambdaUpdateWrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        //2.3 封装修改的列
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);
        //不加where条件，某个租约。 定时任务，检测的是所有的租约，达到租约的时间了，就更改租约状态
        //3.更新
        leaseAgreementService.update(lambdaUpdateWrapper);
    }
}
